import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '../views/login.vue'
import Users from '../components/user/users.vue'
import Roles from '../components/power/roles.vue'
import Goods from '../components/goods/goods.vue'
import Add from '../components/goods/add.vue'
import Update from '../components/goods/update.vue'
import Params from '../components/goods/params.vue'
import Categories from '../components/goods/categories.vue'
import Rights from '../components/power/rights.vue'
import Reports from '../components/reports/reports.vue'
import Orders from '../components/orders/orders.vue'
import Welcome from '../components/welcome/welcome.vue'

Vue.use(VueRouter)

const routes = [{
        path: '/',
        name: 'Login',
        component: Login
    }, {
        path: '/home',
        name: 'Home',
        component: Home,
        children: [{
                path: '/home',
                name: 'Welcome',
                component: Welcome
            },
            {
                path: '/users',
                name: 'Users',
                component: Users
            },
            {
                path: '/roles',
                name: 'Roles',
                component: Roles
            },
            {
                path: '/rights',
                name: 'Rights',
                component: Rights
            },
            {
                path: '/orders',
                name: 'Orders',
                component: Orders
            },
            {
                path: '/goods',
                name: 'Goods',
                component: Goods
            },
            {
                path: '/params',
                name: 'Params',
                component: Params
            },
            {
                path: '/reports',
                name: 'Reports',
                component: Reports
            },
            {
                path: '/categories',
                name: 'Categories',
                component: Categories
            },
            {
                path: '/goods/add',
                name: 'Add',
                component: Add
            }, {
                path: '/goods/update/:id',
                name: 'Update',
                component: Update
            }
        ]
    },

]

const router = new VueRouter({
    routes
})

// to: 即将要进入的目标 路由对象;（这个对象中包含name，params，meta等属性）

// from: 当前导航正要离开的路由对象；（这个对象中包含name，params，meta等属性）

// next: Function: 确保要调用 next 方法，否则钩子就不会被 resolved。
//路由守卫
router.beforeEach((to, from, next) => {
    if (to.path === '/') {
        return next()
    }
    //获取token值
    var strtoken = window.sessionStorage.getItem('token')
    if (!strtoken) {
        return next('/')
    }
    next()
})


export default router